package com.example.smart_light.socket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class ConnSocket extends Thread{
	private static List<Socket> socketList = new CopyOnWriteArrayList<>();
	private ServerSocket serverSocket;


	public ConnSocket() {
		try {
			//监听端口
			serverSocket = new ServerSocket(8888);
			System.out.println("启动端口8888....");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		while(true) {
			try {
				//等待客户端连接
				Socket socket = serverSocket.accept();
				socketList.add(socket);
				System.out.println(socket.getInetAddress()+":"+socket.getPort()+"连接成功");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static List<Socket> getSocketList() {
		return socketList;
	}

}
